package logic

import (
	"context"
	"doctor/doctorrpc/doctorrpc"
	"doctor/doctorrpc/internal/svc"
	"doctor/models"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *AddLogic) Add(in *doctorrpc.AddRequest) (*doctorrpc.AddResponse, error) {
	// todo: add your logic here and delete this line

	doc := models.Doctor{
		Name:  in.Name,
		Image: in.Image,
		Job:   in.Job,
		Age:   int(in.Age),
	}

	models.DB.Debug().Where("name = ?", in.Name).Find(&doc)
	if doc.ID != 0 {
		return nil, fmt.Errorf("当前医生已存在")
	}

	return &doctorrpc.AddResponse{
		DoctorId: int64(doc.ID),
	}, nil
}
